<?php

namespace app\admin\logic;

use nx\Data;
use think\Cache;
use app\admin\model\Options as Options_model;

/**
 * Class Options 设置操作数据
 * @package app\admin\logic
 * @author: Huang
 */
class Options
{
    public $Options_model = '';

    /**获取模型
     * @return Options_model|string
     */
    public function getModel()
    {
        if (empty($this->Options_model)) {
            $this->Options_model = new Options_model();
        }
        return $this->Options_model;
    }

    /**获取所有的设置设置（供获取器使用）
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function OptionssToText()
    {
        $Optionss = self::getAllOptionss();
        $Optionss = collect($Optionss)->column('name', 'id');
        return $Optionss;
    }

    /**将所有设置进行树形排列
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getAllOptionsToTree()
    {
        $Optionss = $this->getAllOptionss();
        $Optionss = Data::tree($Optionss, 'title', 'id', 'pid');
        return $Optionss;
    }

    /**获得所有的设置设置（从缓存中）
     * @return false|mixed|\PDOStatement|string|\think\Collection   返回数组
     */
    public function getAllOptionss()
    {
        $Optionss = Cache::get('allOptionss');
        if (!empty($Optionss)) return $Optionss;
        $Optionss = $this->getModel()->order('sort')->select();
        if (empty($Optionss)) return $Optionss;
        $Optionss = collection($Optionss)->append(['status_text'])->toArray();
        Cache::set('allOptionss', $Optionss);
        return $Optionss;
    }


    /**
     * 获取设置 select 数据
     * @return array
     * @author: Huang
     */
    public function getAllText()
    {
        $Optionss = $this->getAllOptionss();
        $companys = collect($Optionss)->column('title', 'id');
        return $companys;
    }

    /**
     * 获取某设置的所有下属设置
     * @param $Options_id
     * @return array|mixed
     */
    public function getNextOptionss($Options_id)
    {
        $data = $this->getAllOptionss();
        $Optionss = Data::channelList($data, $Options_id, '— ', 'id', 'pid');
        return $Optionss;
    }


    public function addOptions($data)
    {
        if (empty($data)) {
            return ['result' => false, 'msg' => '数据丢失'];
        }
        if (!empty($data['options'])) {
            $data['option_value'] = json_encode($data['options']);
        }
        $result = $this->getModel()->allowField(true)->validate(true)->save($data);
        if (false === $result) {
            return ['result' => false, 'msg' => $this->getModel()->getError()];
        } else {
            Cache::clear();
            return ['result' => true, 'msg' => '配置添加成功'];
        }
    }

    public function editOptions($data)
    {
        if (empty($data['id'])) {
            return ['result' => false, 'msg' => 'ID丢失'];
        }
        isset($data['status']) ?: $data['status'] = 1;
        if (!empty($data['options'])) {
            $data['option_value'] = json_encode($data['options']);
        }else{
            $data['option_value']='';
        }
        $result = $this->getModel()->allowField(true)->validate(true)->isUpdate(true)->save($data);
        if (false === $result) {
            return ['result' => false, 'msg' => $this->getModel()->getError()];
        } else {
            Cache::clear();
            return ['result' => true, 'msg' => '设置修改成功'];
        }
    }

    public function delOptions($id)
    {
        if (empty($id)) {
            return ['result' => false, 'msg' => '请选择要操作的数据'];
        }
        if (Options_model::get($id)) {
            if (Options_model::destroy($id)) {
                Cache::clear();
                return ['result' => true, 'msg' => '删除成功'];
            } else {
                return ['result' => false, 'msg' => '删除失败'];
            }
        } else {
            return ['result' => false, 'msg' => '要删除的数据不存在'];
        }
    }

}
